import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:何人亮
 * Date:2024-11-21
 * 22:19
 */

  class ListNode {
   int val;
   ListNode next = null;
   public ListNode(int val) {
     this.val = val;
   }
 }

public class Test {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
     */
    public ListNode ReverseList (ListNode head) {
        // write code here
        // if(head==null||head.next==null){
        //     return head;
        // }
        // ListNode curPrev=null;
        // ListNode curN=head;
        // while(curN!=null){
        //     ListNode cur=curN.next;
        //     curN.next=curPrev;
        //     curPrev=curN;
        //     curN=cur;
        // }
        // return curPrev;
        Stack<ListNode> stack=new Stack<>();
        if(head==null){
            return head;
        }
        while(head!=null){
            stack.add(head);
            head=head.next;
        }
        ListNode nNode=stack.pop();
        ListNode node=nNode;
        while(!stack.isEmpty()){
            ListNode tmp= stack.pop();
            node.next=tmp;
            node=node.next;
        }
        node.next=null;//不要忘记最后一个节点要指向null
        return nNode;
    }
}